const Controller = require('egg').Controller;

class HomeController extends Controller {
  /// 获取列表数据
  async getList() {
    const { ctx, app } = this
    const data = await ctx.service.home.getList()
    if (typeof data == 'string') {
      ctx.body = {
        code: -1,
        message: data
      }
    } else {
      ctx.body = {
        code: 1,
        data
      }
    }
  }

  /// 获取直播数据
  async getLive() {
    const { ctx, app } = this
    const data = await ctx.service.home.getLive()
    if (typeof data == 'string') {
      ctx.body = {
        code: -1,
        message: data
      }
    } else {
      ctx.body = {
        code: 1,
        data
      }
    }
  }

  /// 获取团购列表数据
  async getGrop() {
    const { ctx, app } = this
    const data = await ctx.service.home.getGrop()
    if (typeof data == 'string') {
      ctx.body = {
        code: -1,
        message: data
      }
    } else {
      ctx.body = {
        code: 1,
        data
      }
    }
  }

  /// 添加数据
  async addList() {
    const { ctx, app } = this
    const data = await ctx.service.home.addList()
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: '添加成功'
      }
    } else {
      ctx.body = {
        code: -1,
        message: '添加失败'
      }
    }
  }

  /// 添加团购数据
  async addGrop() {
    const { ctx, app } = this
    const data = await ctx.service.home.addGrop()
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: '添加成功'
      }
    } else {
      ctx.body = {
        code: -1,
        message: '添加失败'
      }
    }
  }

  /// 编辑数据
  async editList() {
    const { ctx, app } = this
    const data = await ctx.service.home.editList()
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: '编辑成功'
      }
    } else {
      ctx.body = {
        code: -1,
        message: '编辑失败'
      }
    }
  }

  /// 编辑团购数据
  async editGrop() {
    const { ctx, app } = this
    const data = await ctx.service.home.editGrop()
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: '编辑成功'
      }
    } else {
      ctx.body = {
        code: -1,
        message: '编辑失败'
      }
    }
  }

  /// 删除数据
  async delList() {
    const { ctx, app } = this;
    const data = await ctx.service.home.delList()
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: '删除成功'
      }
    } else {
      ctx.body = {
        code: -1,
        message: '删除失败'
      }
    }
  }

  /// 删除直播数据
  async delLive() {
    const { ctx, app } = this;
    const data = await ctx.service.home.delLive()
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: '删除成功'
      }
    } else {
      ctx.body = {
        code: -1,
        message: '删除失败'
      }
    }
  }

  /// 删除团购数据
  async delGrop() {
    const { ctx, app } = this;
    const data = await ctx.service.home.delGrop()
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: '删除成功'
      }
    } else {
      ctx.body = {
        code: -1,
        message: '删除失败'
      }
    }
  }

  /// 详情数据
  async detailId() {
    const { ctx, app } = this
    const data = await ctx.service.home.detailId()
    ctx.body = data
  }

  /// 直播搜索
  async Search() {
    let { ctx, app } = this;
    let data = await ctx.service.home.Search();
    ctx.body = data;
  }

  /// 判断直播列表数据
  async switchLive() {
    const { ctx, app } = this
    const data = await ctx.service.home.switchLive()
    if (typeof data == 'string') {
      ctx.body = {
        code: -1,
        message: data
      }
    } else {
      ctx.body = {
        codE: 1,
        data
      }
    }
  }

}

module.exports = HomeController;